import copy
import os
import glob
import polib

# 获取当前Python文件所在的目录
current_dir = os.path.dirname(os.path.abspath(__file__))

# 使用glob模块获取该目录下所有的*.mo文件
mo_files_name = glob.glob(os.path.join(current_dir, "*.mo"))

from_name_list = []
to_name_list = []
with open('vehicle_rename.txt', 'r', encoding='utf-8') as file:
    for line in file:
        # 去除行末的换行符，并按空格分割字符串
        parts = line.strip().split()
        if len(parts) == 2:
            # 将第一个字符串添加到from_name列表，第二个添加到to_name列表
            from_name_list.append(parts[0])
            to_name_list.append(parts[1])

# 输出所有.mo文件的文件名
for name in mo_files_name:
    if 'vehicles' in name:
        print(f'processing {os.path.basename(name)}')
        mo = polib.mofile(name)
        for entry in mo:
            original_str = entry.msgstr
            for from_name, to_name in zip(from_name_list, to_name_list):
                if from_name in original_str and len(original_str) <= 20:
                    # Replace occurrences of from_name with to_name
                    updated_str = original_str.replace(from_name, to_name)
                    print(f'Renamed "{original_str}" to "{updated_str}"')
                    entry.msgstr = updated_str
        mo.save(name)
